package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("SupplierDAO")
@Transactional
public class SupplierDAOImpl extends AbstractJpaDao<Supplier> implements SupplierDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Supplier.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public SupplierDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Supplier findSupplierById(Integer id) throws DataAccessException {

		return findSupplierById(id, -1, -1);
	}

	@Transactional
	public Supplier findSupplierById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSupplierById", startResult, maxRows, id);
			return (Supplier) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Supplier> findSupplierByName(String name) throws DataAccessException {

		return findSupplierByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Supplier> findSupplierByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSupplierByName", startResult, maxRows, name);
		return new LinkedHashSet<Supplier>(query.getResultList());
	}

	@Transactional
	public Set<Supplier> findSupplierByNameContaining(String name) throws DataAccessException {

		return findSupplierByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Supplier> findSupplierByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSupplierByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Supplier>(query.getResultList());
	}

	@Transactional
	public Set<Supplier> findSupplierByDescription(String description) throws DataAccessException {

		return findSupplierByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Supplier> findSupplierByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSupplierByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Supplier>(query.getResultList());
	}

	@Transactional
	public Set<Supplier> findSupplierByDescriptionContaining(String description) throws DataAccessException {

		return findSupplierByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Supplier> findSupplierByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSupplierByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Supplier>(query.getResultList());
	}

	@Transactional
	public Supplier findSupplierByPrimaryKey(Integer id) throws DataAccessException {
		return findSupplierByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Supplier findSupplierByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSupplierByPrimaryKey", startResult, maxRows, id);
			return (Supplier) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Supplier> findSuppliersByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSuppliersByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Supplier> findAllSuppliers() throws DataAccessException {

		return findAllSuppliers(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Supplier> findAllSuppliers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllSuppliers", startResult, maxRows);
		return new LinkedHashSet<Supplier>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Supplier> findSuppliersByAddress(Integer addressId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllSuppliersByAddress", start, max, addressId);
		return new LinkedHashSet<Supplier>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Supplier> findSupplierByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Supplier>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Supplier> findSuppliersByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Supplier.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Supplier> suppliers = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return suppliers;
	}

	@Transactional
	public Supplier findSupplierByPhysicalPrimaryKey(String name, Integer addressId) throws DataAccessException {
		return findSupplierByPhysicalPrimaryKey(name, addressId, -1, -1);
	}

	@Transactional
	public Supplier findSupplierByPhysicalPrimaryKey(String name, Integer addressId, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findSupplierByPhyscialPrimaryKey", startResult, maxRows, name, addressId);
			return (Supplier) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Supplier entity) {
		return true;
	}
}
